package jerry.cn.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jerry.cn.common.Adapter.ShowStringTypeAdapter;
import jerry.cn.common.Bean.BarCodeMessage;
import jerry.cn.common.Bean.SingleStringBean;
import jerry.cn.common.Port.ScanSingleModule;
import jerry.cn.common.Tools.ASCII_Tools;
import jerry.cn.common.View.RvLinearManageDivider;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.util.Log;

public class ShowCodeNumberActivity extends BaseActivity implements OnBottomDragListener {

    private TextView Tv_ShowCode;
    private TextView Tv_CodeNumber;
    private RecyclerView RV_StringType;

    private Context MContect;
    private static ScanSingleModule scanSingleModule;

    private ShowStringTypeAdapter showStringTypeAdapter;
    private List<SingleStringBean> singleStringBeanList = new ArrayList<>();

    public static Intent createIntent(Context context, ScanSingleModule scanSingleModule) {
        ShowCodeNumberActivity.scanSingleModule = scanSingleModule;
        return new Intent(context, ShowCodeNumberActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_scancode_number, this);
        MContect = new WeakReference<>(ShowCodeNumberActivity.this).get();
        EventBus.getDefault().register(MContect);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }

    @Override
    public void initView() {
        RV_StringType = findView(R.id.RV_StringType);
        Tv_ShowCode = findView(R.id.Tv_ShowCode, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        Tv_CodeNumber = findView(R.id.Tv_CodeNumber);

        LinearLayoutManager ScanTaskL = new LinearLayoutManager(this);
        ScanTaskL.setOrientation(ScanTaskL.VERTICAL);
        RV_StringType.addItemDecoration(new RvLinearManageDivider(this, LinearLayoutManager.VERTICAL));
        RV_StringType.setLayoutManager(ScanTaskL);
        showStringTypeAdapter = new ShowStringTypeAdapter(this, singleStringBeanList);
        RV_StringType.setAdapter(showStringTypeAdapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        scanSingleModule.StartSingle();
    }

    @Override
    public void onDragBottom(boolean rightToLeft) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(MContect);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventBus(BarCodeMessage msg) {
        showProgressDialog("计算中");
        singleStringBeanList.clear();
        Tv_ShowCode.setText(msg.data);
        Tv_CodeNumber.setText(String.valueOf(Tv_ShowCode.getText().toString().length()));
        for (int i = 0; i < msg.data.length(); i++) {
            SingleStringBean singleStringBean = new SingleStringBean();
            singleStringBean.setDigit(String.valueOf(i + 1));
            singleStringBean.setString(String.valueOf(msg.data.charAt(i)));
            singleStringBean.setASCII(String.valueOf(ASCII_Tools.string2ASCII(String.valueOf(msg.data.charAt(i)))[0]));
            
            Pattern p = Pattern.compile("[0-9]*");
            Matcher m = p.matcher(String.valueOf(msg.data.charAt(i)));
            if (m.matches()) {
                singleStringBean.setType("数字");
            }
            p = Pattern.compile("[a-zA-Z]");
            m = p.matcher(String.valueOf(msg.data.charAt(i)));
            if (m.matches()) {
                singleStringBean.setType("英文");
            }
            p = Pattern.compile("[\u4e00-\u9fa5]");
            m = p.matcher(String.valueOf(msg.data.charAt(i)));
            if (m.matches()) {
                singleStringBean.setType("汉字");
            }
            singleStringBeanList.add(singleStringBean);
        }
        showStringTypeAdapter.notifyDataSetChanged();
        dismissProgressDialog();
        Log.i("jerry.cn.common = ", "messageEventBus Tv_ShowCode.getText().toString().length() = " + Tv_ShowCode.getText().toString().length());
    }
    
}
